package org.easyspring.core;

import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.easyspring.core.annotation.Autowired;
import org.easyspring.util.ClassUtil;
import org.easyspring.util.ValidationUtil;

import java.lang.reflect.Field;
import java.util.Set;

/**
 * Created by Hy on 2021/7/31.
 */
@Slf4j
public class DependencyInjector {
    private BeanContainer beanContainer;

    public DependencyInjector() {
        this.beanContainer = BeanContainer.getInstance();
    }

    /**
     * 执行Ioc TODO【0004】【★★★★★】
     */
    public void doIoc(){
        if(ValidationUtil.isEmpty(beanContainer.getClasses())){
            log.warn("empty classset in BeanContainer");
            return;
        }
        //1.遍历bean容器中的所有class对象
        for (Class<?> clazz:beanContainer.getClasses()
             ) {
            //2.遍历class对象的所有成员变量
            Field[] fields = clazz.getDeclaredFields();
            if(ValidationUtil.isEmpty(fields)){
                continue;
            }
            for (Field field:fields
                 ) {
                //3.找出被Autowired标记的成员变量
                if(field.isAnnotationPresent(Autowired.class)){
                    //4.获取这些成员变量的类型
                    Class<?> fieldClass = field.getType();
                    Autowired autowired = field.getAnnotation(Autowired.class);
                    String autowiredValue = autowired.value();
                    //5.获取这些成员变量的类型容器中对应的实例
                    Object fieldValue = getFieldInstance(fieldClass,autowiredValue);
                    if(fieldValue == null){
                        throw new RuntimeException("unable to inject relevant type,target fieldClass is:"
                                +fieldClass.getName()+"autowiredValue is:"+autowiredValue);
                    }else{
                        //6.通过反射将对应的成员变量实例注入到成员变量所在类的实例里
                        Object targetBean = beanContainer.getBean(clazz);
                        ClassUtil.setField(field,targetBean,fieldValue,true);
                    }
                }
               
            }
           
        }
       
    }

    /**
     * 根据class在beanContainer里获取其实例或者实现类
     * @param fieldClass
     * @return
     */
    private Object getFieldInstance(Class<?> fieldClass,String autowiredValue) {
        Object fieldValue = beanContainer.getBean(fieldClass);
        if(fieldValue!=null){
            return fieldValue;
        }else{

            Class<?> implementedClass = getImplementClass(fieldClass,autowiredValue);
            if(implementedClass!=null){
                return beanContainer.getBean(implementedClass);
            }else{
                return null;
            }
        }
    }

    /**
     * 获取接口的实现类
     * @param fieldClass
     * @param autowiredValue
     * @return
     */
    private Class<?> getImplementClass(Class<?> fieldClass,String autowiredValue) {
        Set<Class<?>> classSet = beanContainer.getClassBySuper(fieldClass);
        if(!ValidationUtil.isEmpty(classSet)){
            if(ValidationUtil.isEmpty(autowiredValue)){
                if(classSet.size()==1){
                    return classSet.iterator().next();
                }else{
                    //如果多于两个实现类且用户未指定其中一个实现类,则抛出异常
                    throw new RuntimeException("multiple implemented classes for "
                            +fieldClass.getName()+"please set @Autowired's to pick one");
                }
            }else{
                for (Class<?> clazz:classSet
                     ) {
                    if(autowiredValue.equals(clazz.getSimpleName())){
                        return clazz;
                    }
                }
            }
        }
        return null;
    }
}
